package com.leetcode.algorithm.y22.m08.w3;

import com.leetcode.algorithm.common.TreeNode;

/**
 * 530. 二叉搜索树的最小绝对差
 * 
 * https://leetcode.cn/problems/minimum-absolute-difference-in-bst/
 * 
 * @author jie.deng
 *
 */
class Question0530Solution01 {

	public int getMinimumDifference(TreeNode root) {
		return getMinimumDifference(root, new TreeNode[] { null }, Integer.MAX_VALUE);
	}

	private int getMinimumDifference(TreeNode root, TreeNode[] arr, int min) {
		if (root == null) {
			return min;
		}
		min = getMinimumDifference(root.left, arr, min);
		if (arr[0] != null) {
			min = Math.min(min, root.val - arr[0].val);
		}
		arr[0] = root;
		min = getMinimumDifference(root.right, arr, min);
		return min;
	}

}